package com.xy.exam.service.impl;

import com.xy.exam.Exception.ServiceException;
import com.xy.exam.dao.mapper.ExamMapper;
import com.xy.exam.dao.mapper.ExamUserMapper;
import com.xy.exam.pojo.dto.ExamAddNewDTO;
import com.xy.exam.pojo.dto.ExamUpdateDTO;
import com.xy.exam.pojo.dto.FrontCheckResultExamDTO;
import com.xy.exam.pojo.entity.Exam;
import com.xy.exam.pojo.entity.ExamUser;
import com.xy.exam.pojo.entity.Info;
import com.xy.exam.pojo.vo.*;
import com.xy.exam.service.IExamService;
import com.xy.exam.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {
    @Autowired
    ExamMapper examMapper;

    @Autowired
    ExamUserMapper examUserMapper;
    @Override
    public void addNew(ExamAddNewDTO examAddNewDTO) {
        String title = examAddNewDTO.getTitle();
        int countByTitle = examMapper.countByTitle(title);
        if (countByTitle > 0) {
            String message = "添加考试失败,该考试标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Exam exam = new Exam();
        exam.setStatus(0);
        exam.setCreateTime(new Date());
        exam.setUpdateTime(new Date());
        BeanUtils.copyProperties(examAddNewDTO, exam);
        int insert = examMapper.insert(exam);
        if (insert != 1) {
            String message = "添加考试失败,该考试标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        ExamStandardVO standardById = examMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除考试失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int deleteById = examMapper.deleteById(id);
        if (deleteById !=1) {
            String message = "删除考试失败，服务器忙，请稍后再尝试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateExamById(Long id, ExamUpdateDTO examUpdateDTO) {
        ExamStandardVO standardById = examMapper.getStandardById(id);
        if (standardById == null) {
            String message = "修改考试详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = examUpdateDTO.getTitle();
        int count = examMapper.countByTitleAndNotId(id,name);
        if (count > 0) {
            String message = "修改考试详情失败，考试名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Exam exam = new Exam();
        exam.setUpdateTime(new Date());
        exam.setId(id);
        BeanUtils.copyProperties(examUpdateDTO,exam);
        int rows = examMapper.update(exam);
        if (rows != 1) {
            String message = "修改考试详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ExamStandardVO getStandardById(Long id) {
        ExamStandardVO standardById = examMapper.getStandardById(id);
        if (standardById == null) {
            String message = "获取用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardById;
    }

    @Override
    public List<ExamListItemVO> list() {
        List<ExamListItemVO> list = examMapper.list();
        return list;
    }

    @Override
    public FrontCheckResultExamVO checkResult(FrontCheckResultExamDTO frontCheckResultExamDTO) {
        String applyName = frontCheckResultExamDTO.getApplyName();
        Long id = frontCheckResultExamDTO.getId();
        int count = examUserMapper.countByApplyNameAndExamId(applyName, id);
        if (count != 1) {
                String message = "您输入的信息有误，请输入正确信息！";
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //获取考试项目名称
        ExamStandardVO standardById = examMapper.getStandardById(id);
        if (standardById == null) {
            String message = "您输入的信息有误，该考试信息不存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //获取考生信息
        ExamUser userByApplyName = examUserMapper.getExamUserByApplyName(applyName,id);
        if (userByApplyName == null) {
            String message = "您输入的信息有误，该考生信息不存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        FrontCheckResultExamVO examVO = new FrontCheckResultExamVO();
        examVO.setTitle(standardById.getTitle());
        if (userByApplyName.getScore() == null || userByApplyName.getScore().isEmpty()) {
            examVO.setScore("该考试成绩暂未公布！");
        }else {
            examVO.setScore(userByApplyName.getScore());
        }
        examVO.setApplyName(applyName);
        return examVO;
    }
}
